package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.utli.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private  ApplicationContext ctx;


    @Override
    public void reload() {
        List<Permission> permissions = permissionMapper.selectAll();
        Set<String> expressions=new HashSet<>();
        for (Permission permission : permissions) {
            expressions.add(permission.getExpression());
        }


        //通过Controller获取带有注解的方法,转换成对象
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        //在用spring上下文获取所有的bean
        Collection<Object> values = beans.values();
        //遍历集合
        for (Object value : values) {
            //通过字节码对象拿到所有的方法
            Method[] declaredMethods = value.getClass().getDeclaredMethods();
            for (Method declaredMethod : declaredMethods) {
                //获取方法的注解
                RequiredPermission annotation = declaredMethod.getAnnotation(RequiredPermission.class);
                if (annotation!=null){
                    //获取方法注解name和expression
                    String name = annotation.name();
                    String expression = annotation.expression();

                    if (!expressions.contains(expression)){
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }

    @Override
    public List<String> queryByEmployeeId(Long employeeId) {

        return permissionMapper.selectByEmployeeId(employeeId);
    }

    @Override
    public void deleteByPrimaryKey(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Permission record) {
        permissionMapper.insert(record);
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {

        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> selectAll() {

        return permissionMapper.selectAll();
    }

    @Override
    public void updateByPrimaryKey(Permission record) {
        permissionMapper.updateByPrimaryKey(record);

    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentpage(),qo.getPageSize() );

        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo<Permission>(permissions);
    }


}
